package com.cgc.venus.server.user.service;

import com.cgc.venus.server.core.base.AbstractBaseService;
import com.cgc.venus.server.core.bean.page.PageQuery;
import com.cgc.venus.server.core.exception.VenusException;
import com.cgc.venus.server.core.utils.StringUtil;
import com.cgc.venus.server.user.bean.RoleBean;
import com.cgc.venus.server.user.inter.RoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.EmptyResultDataAccessException;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by xyr on 2017/9/11.
 * 角色维护,dao实现层
 */
@Service
public class RoleServiceImpl extends AbstractBaseService implements RoleService{

    @Autowired
    private JdbcTemplate jdbcTemplate;

    /*
    * 登录角色id,name,填充选择下拉框
    * */
    @Override
    public List<Map<String, Object>> getRoleToSelect() throws VenusException {
        String sql = "SELECT id,name from t_role";
        List<Map<String,Object>> mapList = new ArrayList<Map<String, Object>>();
        try{
            mapList = jdbcTemplate.queryForList(sql);
        }catch (EmptyResultDataAccessException ex){
            return mapList;
        }
        return mapList;
    }

    /*
    * 得到角色信息
    * */
    @Override
    public List<Map<String, Object>> getRole(PageQuery pageQuery) throws VenusException {
        String sql = "SELECT id, name, described, is_superadmin AS isSuperAdmin, r.function FROM t_role r " +
                " ORDER BY last_update_time DESC  LIMIT ?,?";
        List<Map<String,Object>> mapList = new ArrayList<Map<String, Object>>();
        List<Object> objects = new ArrayList<Object>();
        int page = (pageQuery.getPage()-1) * pageQuery.getPageSize();
        objects.add(page);
        objects.add(pageQuery.getPageSize());
        try{
            mapList = jdbcTemplate.queryForList(sql,objects.toArray());
        }catch (EmptyResultDataAccessException ex){
            return mapList;
        }
        return mapList;
    }

    /*
    * 新增角色信息
    * */
    @Override
    public int addRole(RoleBean roleBean) throws VenusException {
        String sql = "INSERT INTO t_role (id,name,described,is_superadmin,function,create_time,last_update_time) VALUES(?,?,?,?,?,NOW(),NOW())";
        List<Object> list = new ArrayList<Object>();
        list.add(StringUtil.getUUID());
        list.add(roleBean.getName());
        list.add(roleBean.getDescribed());
        int isSuperAdmin = roleBean.getIsSuperAdmin() ? 1 : 0;
        list.add(isSuperAdmin);
        list.add(roleBean.getFunction());
        int row = jdbcTemplate.update(sql,list.toArray());
        return row;
    }

    /*
    * 更新角色信息,角色名字不能更新
    * */
    @Override
    public int updateRole(RoleBean roleBean) throws VenusException {
        String sql = "UPDATE t_role SET described=?, is_superadmin=?, function=?, last_update_time=NOW() WHERE id=? ";
        List<Object> list = new ArrayList<Object>();
        list.add(roleBean.getDescribed());
        int isSuperAdmin = roleBean.getIsSuperAdmin() ? 1 : 0;
        list.add(isSuperAdmin);
        list.add(roleBean.getFunction());
        list.add(roleBean.getId());
        int row = jdbcTemplate.update(sql,list.toArray());
        return row;
    }

    /*
    * 删除角色信息
    * */
    @Override
    public int deleteRole(String id) throws VenusException {
        String sql = "DELETE FROM t_role WHERE id=?";
        List<Object> list = new ArrayList<Object>();
        list.add(id);
        int row = jdbcTemplate.update(sql,list.toArray());
        return  row;
    }

    @Override
    public boolean exist(String name) throws VenusException {
        String sql = "select id from t_role  where name=?  ";
        List<Object> list = new ArrayList<Object>();
        list.add(name);
        Map<String,Object> map = new HashMap<String, Object>();
        try{
            map = jdbcTemplate.queryForMap(sql,list.toArray());
        }catch (EmptyResultDataAccessException ex){
            return false;
        }
        if (map.size() == 0) return false;
        return true;
    }

    @Override
    public long countRole() throws VenusException {
        long count = 0;
        Map<String,Object> map = null;
        String sql = "SELECT COUNT(*) AS count  FROM t_role";
        try {
            map = jdbcTemplate.queryForMap(sql);
        } catch (EmptyResultDataAccessException EX){
            return count;
        }
        if (map.size() > 0){
            count = (Long) map.get("count");
        }
        return count;
    }

    /*
    * 根据角色ID,获得所有属于这个角色的用户,删除角色的时候,要用到这个方法做判断
    * */
    @Override
    public List<Map<String, Object>> getUserByRoleId(String id) throws VenusException {
        String sql = "SELECT id FROM t_user WHERE role = ?";
        List<Object> objects = new ArrayList<Object>();
        objects.add(id);
        List<Map<String,Object>> mapList = new ArrayList<Map<String, Object>>();
        try {
            mapList = jdbcTemplate.queryForList(sql,objects.toArray());
        } catch (EmptyResultDataAccessException ex ) {
            return mapList;
        }
        return mapList;
    }
}
